<?php
/**
 * Addressbook.php
 * 通讯录 API
 * @author Deepseath
 * @version $Id$
 */
namespace deepseath\huoban\Api;

class Addressbook
{
    /**
     * 基类服务对象
     * @var \deepseath\huoban\Huoban
     */
    protected $service = null;

    /**
     * 企业ID
     *
     * @var string
     */
    protected $companyId = '';

    public function __construct(\deepseath\huoban\Huoban $service)
    {
        $this->service = $service;
        $this->companyId = $service->config()['company_id'];
    }

    /**
     * 获取通讯录成员列表
     *
     * @see https://api.huoban.com/v2/company_members/company/:company_id
     * @param array $param
     * + company_department_id: 获取指定部门下的成员列表。设置此参数时会返回当前部门和子部门的成员列表合集。为空表示获取全部
     * + limit 本页条目数
     * + offset 偏移量，用于分页
     * + search 搜索关键字，支持姓名、邮箱、手机号码
     * + status 成员状态，joined：已加入企业；unactive：未激活
     * @return array
     */
    public function userList(array $param = [])
    {
        $path = 'company_members/company/' . $this->companyId;

        $options = [];
        $options['offset'] = $param['offet'] ?? 0;
        $options['limit'] = $param['limit'] ?? 10;
        if (isset($param['company_department_id'])) {
            $options['company_department_id'] = $param['company_department_id'];
        }
        if (isset($param['search'])) {
            $options['search'] = $param['search'];
        }
        if (isset($param['status']) && in_array($param['status'], ['joined', 'unactive'])) {
            $options['status'] = $param['status'];
        }

        return $this->service->apiRequest('post', $path, $options);
    }
}
